package com.smile.lyh.service;

import com.smile.lyh.MyValue;
import com.smile.spring.BeanPostProcessor;
import com.smile.spring.Component;

import java.lang.reflect.Field;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2021/12/11 17:55
 */
@Component
public class MyValueBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {

        for (Field field : bean.getClass().getDeclaredFields()) {
            if (field.isAnnotationPresent(MyValue.class)) {
                field.setAccessible(true);
                try {
                    field.set(bean, field.getAnnotation(MyValue.class).value());
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }

        // bean
        return bean;
    }
}
